﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Rendering;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Finance
{
    public class Finance_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Finance/Finance_Edit.cshtml";
        protected string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        public IUsersService _users { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("finance_list", CMSEnums.ActionEnum.View.ToString()); //检查权限
            string _action = Request.Query["action"];
            if (!string.IsNullOrEmpty(_action) && _action == CMSEnums.ActionEnum.Edit.ToString())
            {
                this.action = CMSEnums.ActionEnum.Edit.ToString();//修改类型
                int.TryParse(Request.Query["id"], out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = View(EDIT_RESULT_VIEW);
                    return;
                }
                if (!_users.Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    filterContext.Result = View(EDIT_RESULT_VIEW);
                    return;
                }
            }
            ViewBag.Id = this.id;
            ViewBag.Action = this.action;
        }
        // GET: /admin/User_Edit/

        public ActionResult Index()
        {
            TreeBind(); //会员级别
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            else
            {
                NetCoreCMS.Models.Users model = new NetCoreCMS.Models.Users();
                ViewData["model"] = model;
            }
            return View(WEB_VIEW);
        }

        #region 绑定组别=================================
        private void TreeBind()
        {
            List<SelectListItem> levelListItems = new List<SelectListItem>();
            levelListItems.Add(new SelectListItem() { Text = "请选择级别...", Value = "" });            
            levelListItems.Add(new SelectListItem() { Text = "银牌会员", Value = "10" });
            levelListItems.Add(new SelectListItem() { Text = "金牌会员", Value = "20" });
            levelListItems.Add(new SelectListItem() { Text = "钻石会员", Value = "30" });
            levelListItems.Add(new SelectListItem() { Text = "合作伙伴", Value = "40" });
            ViewData["levelListItems"] = levelListItems;
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var model = _users.GetModel(_id);
            var duser = _users.GetModel(model.pid);
            var iuser = _users.GetModel(model.iid);
            ViewBag.direct = "不存在";
            ViewBag.indirect = "不存在";
            if (duser != null)
                ViewBag.direct = duser.user_name;
            if (iuser != null)
                ViewBag.indirect = iuser.user_name;
            ViewData["model"] = model;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            var model = _users.GetModel(_id);

            model.level_id = int.Parse(Request.Form["ddlLevelId"]);
            model.is_jiangjin = int.Parse(Request.Form["rblJiangjin"]);
            model.is_kong = int.Parse(Request.Form["rblKong"]);
            model.is_lock = int.Parse(Request.Form["rblLock"]);
            model.is_share = int.Parse(Request.Form["rblShare"]);
            //duser
            string duser_name = CMSRequest.GetFormString("txtDirect", HttpContext);
            var duser = _users.GetModel(duser_name);            
            model.pid = duser.id;
            model.layer = duser.layer + 1;
            //iuser
            string iuser_name = CMSRequest.GetFormString("txtIndirect", HttpContext);
            var iuser = _users.GetModel(iuser_name);
            model.iid = iuser.id;
            model.left_right = int.Parse(Request.Form["rblLeftRight"]);
            model.cash_point = CMSRequest.GetFormDecimal("txtCashPoint", 0, HttpContext);
            model.shop_point = CMSRequest.GetFormDecimal("txtShopPoint", 0, HttpContext);

            if (_users.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改贵宾用户信息:" + model.user_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        [HttpPost]
        public ActionResult SubmitSave()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("finance_list", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                }
                else
                {
                    JscriptMsg("修改贵宾会员成功！", "../finance_list/index");
                }
            }            
            return View(EDIT_RESULT_VIEW);
        }
    }
}
